package com.hbnu.exercise.mysql;

import java.io.IOException;
import java.io.InputStream;
import java.security.PublicKey;
import java.sql.*;
import java.util.Properties;
import java.util.Stack;

public class DBUtilities
{
    private static String driverClass;
    private static String url;
    private static String user;
    private static String password;

    static
    {
        InputStream resourceAsStream = DBUtilities.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        try
        {
            properties.load(resourceAsStream);
            driverClass = properties.getProperty("driverClass");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");

            Class.forName(driverClass);
        } catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    public static Connection GetConnection() throws SQLException
    {
        return DriverManager.getConnection(url ,user, password);
    }

    public static void Close(ResultSet resultSet, Statement statement, Connection connection)
    {
        try
        {
            if (resultSet != null)
                resultSet.close();
        } catch (SQLException e)
        {
            throw new RuntimeException(e);
        }

        try
        {
            if (statement != null)
                statement.close();
        } catch (SQLException e)
        {
            throw new RuntimeException(e);
        }

        try
        {
            if (connection != null)
                connection.close();
        } catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
    }
}
